package com.group9.handinhand.dao.model;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.ArrayUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.group9.handinhand.service.so.RequestSO;

@Entity
@Table(name = "REQUEST")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Request extends Activity {

	private static final long serialVersionUID = 1L;
	
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "EXPIRATION_DATE" , nullable = false)
	private Date expirationDate;
    
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "REQUEST_MODERATOR", joinColumns = @JoinColumn(name = "REQUEST_ID", nullable = false), 
	inverseJoinColumns = @JoinColumn(name = "USER_ID", nullable = false), 
	uniqueConstraints = @UniqueConstraint(columnNames = {"USER_ID", "REQUEST_ID" }))
	private Set<User> moderators = new HashSet<User>();
	
	@OneToMany(mappedBy = "request", fetch = FetchType.LAZY)
	private Set<RequestPost> posts = new HashSet<RequestPost>();
	
	public Set<RequestPost> getPosts() {
		return posts;
	}

	public void setPosts(Set<RequestPost> posts) {
		this.posts = posts;
	}

	public RequestSO convertToServiceObject(String... ignoreProperties) {
		RequestSO so = (RequestSO) convert(new RequestSO(), 
			(String[]) ArrayUtils.addAll(ignoreProperties, new String[]{"community","creator","posts","service","goods","moderators"}));
		if( ! Arrays.asList(ignoreProperties).contains("community"))
			so.setCommunity(super.getCommunity().convertToServiceObject(new String[]{"moderators", "posts", "events","requests","offers"}));
		if( ! Arrays.asList(ignoreProperties).contains("creator"))
			so.setCreator(super.getCreator().convertToServiceObject(new String[]{"socialUsers"}));
		if (!Arrays.asList(ignoreProperties).contains("posts"))
			for (RequestPost post : posts) {
				so.getPosts().add(post.convertToServiceObject());
			}
		if( ! Arrays.asList(ignoreProperties).contains("service"))
			if(super.getService() != null)
				so.setService(super.getService().convertToServiceObject());
		if( ! Arrays.asList(ignoreProperties).contains("goods"))
			if(super.getGoods() != null)
				so.setGoods(super.getGoods().convertToServiceObject());
		if( ! Arrays.asList(ignoreProperties).contains("moderators"))
		for (User user : moderators) {
			so.getModerators().add(user.convertToServiceObject(new String[]{"socialUsers"}));
		}
		return so;
	}
	
	public Date getExpirationDate() {
		return expirationDate;
	}

	public void setExpirationDate(Date expirationDate) {
		this.expirationDate = expirationDate;
	}

	public Set<User> getModerators() {
		return moderators;
	}

	public void setModerators(Set<User> moderators) {
		this.moderators = moderators;
	}

}
